//
//  LTFBeautyView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/27.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFBeautyView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFBeautyModel.h"
#import "LTFFourthTableViewCell.h"
#import "LTFFifthTableVIewCell.h"
#import "LTFSixthTableVIewCell.h"
#define WIDTH self.frame.size.width
@interface LTFBeautyView ()
<
 UITableViewDataSource,
 UITableViewDelegate
>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableViewArray;
@end

@implementation LTFBeautyView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
    [self addSubview:_tableView];
    [_tableView release];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 240;
    _tableView.tag = 1010;
    [_tableView registerClass:[LTFFourthTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LTFFourthTableViewCell class])];
    [_tableView registerClass:[LTFFifthTableVIewCell class] forCellReuseIdentifier:NSStringFromClass([LTFFifthTableVIewCell class])];
    [_tableView registerClass:[LTFSixthTableVIewCell class] forCellReuseIdentifier:NSStringFromClass([LTFSixthTableVIewCell class])];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     LTFBeautyModel *model = self.tableViewArray[indexPath.row];
    if (indexPath.row % 4 == 1 || indexPath.row % 4 == 3) {
        LTFFourthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFFourthTableViewCell class])];
        
        cell.model = model;
        return cell;
    }
    else if (indexPath.row % 4 == 2){
        LTFFifthTableVIewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFFifthTableVIewCell class])];
        
        cell.model = model;
        return cell;
    }
    else {
        LTFSixthTableVIewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFSixthTableVIewCell class])];
        
        cell.model = model;
        return cell;
    }
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFBeautyModel *model = self.tableViewArray[indexPath.row];
    [self.delegate sendAddress:model.setid];
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    LTFBeautyModel *model = self.tableViewArray[indexPath.row];
//    NSArray *array = [model.pixel componentsSeparatedByString:@"*"];
//    CGFloat h = (WIDTH - 8) / [array[0] floatValue] * [array[1] floatValue];
//    return h + 80;
//}
- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string retain];
        [self createData];
    }
}
- (void)createData {
    
    self.tableViewArray = [NSMutableArray array];
    
    NSString *string = @"http://c.3g.163.com/photo/api/list/0096/4GJ60096.json";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject;
        self.tableViewArray = [LTFBeautyModel BaseModelByArray:array];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
